package module.main.view
{
	
	import code.utils.MaterialUtil;
	
	import com.darwin.darwinDebug.FPS;
	import com.darwin.darwinDll.Dll;
	import com.darwin.darwinDll.core.DllItem;
	import com.darwin.darwinIsometric.pathfinding.ANode;
	import com.darwin.darwinIsometric.pathfinding.MapGrid;
	import com.darwin.darwinUI.controls.Button;
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.core.UISprite;
	import com.darwin.darwinUI.manager.UIManager;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	import flash.utils.Timer;
	
	import main.App;
	import main.AppConfig;
	import main.AppControllerList;
	
	import module.main.event.MainEvent;
	import module.main.model.dataObj.UserInfoObj;
	import module.main.view.mainInterface.PlayerInformationPanel;
	import module.main.view.mainInterface.PromptInfoPanel;
	import module.main.view.mainInterface.operatepanel.OperateFriendSango;
	import module.main.view.mainInterface.operatepanel.OperateHomeBack;
	import module.main.view.mainInterface.operatepanel.OperatePanel;
	import module.main.view.mainInterface.playerresourcepanel.ResourceInformationPanel;
	import module.scene.consts.SceneConst;
	import module.shop.view.ProduceItemEffect;
	
	/** 
	 * 主界面
	 * @author lipi
	 */
	public class MainView extends UISprite
	{
		public function MainView()
		{
			super();
			
			if(this.stage)
			{
				init();
			}else{
				addEventListener(Event.ADDED_TO_STAGE,addToStageHandler);
			}
			if(_instance == null)
			{
				_instance = this;
			}
			else{throw new Error("实例化单例类出错-MainInterfaceLayer");}
		}
		
		public function get resourceinfPanel():ResourceInformationPanel
		{
			return _resourceinfPanel;
		}

		public function get operateFt():OperateFriendSango
		{
			return _operateFt;
		}

		private function init():void
		{	
			initView();
			addChild(new FPS());
			
			var contextMenu:ContextMenu = new ContextMenu();
			contextMenu.hideBuiltInItems();
			var contextMenuItemSys:ContextMenuItem = new ContextMenuItem("v1.0.0",false,false);
			contextMenu.customItems.push(contextMenuItemSys);
			var contextMenuItemVer:ContextMenuItem = new ContextMenuItem("20111207_02",false,false);
			contextMenu.customItems.push(contextMenuItemVer);
			var contextMenuItemUid:ContextMenuItem = new ContextMenuItem("uid:"+AppConfig.getInstance().userId,false,false);
			contextMenu.customItems.push(contextMenuItemUid);
			this.contextMenu = contextMenu;
			
			App.appStage.addEventListener(Event.RESIZE,onResize);
		}
		
		private static var _instance:MainView;

		private function addToStageHandler(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,addToStageHandler);
			init();
		}
		public static function getInstance():MainView 
		{
			if(!_instance)
			{
				_instance = new MainView;
			}
			return _instance;	
		}
		private var _loginBox:LoginBox;
		
		//private var _ppp:PromptInfoPanel;
		private var _test:OperatePanel;
		private var _resourceinfPanel:ResourceInformationPanel;
		private var _playInfPanel:PlayerInformationPanel;
		private var _homeBackButton:OperateHomeBack;
		public var _operateFt:OperateFriendSango;
		private function initView():void
		{
			//_ppp = new PromptInfoPanel("tgjdfogjdfkjgdfkjgkldfjgkljdflgjdfjgldfjglkdfjklgjdfklgjkldfjgkdfjklgjdfklgjkldfjgklsdfjgjdfklgjldfjgldfjgdjfgkljdflgjdfljgd");
			_homeBackButton = new OperateHomeBack();
			_operateFt = new OperateFriendSango();
			_operateFt.x = App.appStage.stageWidth - _operateFt.width;
			_operateFt.y = App.appStage.stageHeight - _operateFt.height;
			_homeBackButton.y = App.appStage.stageHeight - _homeBackButton.height;
			_homeBackButton.addEventListener(MouseEvent.CLICK,onHomeClick);
			_test = new OperatePanel();
			_resourceinfPanel = new ResourceInformationPanel();
			_playInfPanel = new PlayerInformationPanel();
			_test.x = (App.appStage.stageWidth - _test.width) / 2;
			_test.y = App.appStage.stageHeight - _test.height;
			_resourceinfPanel.x = App.appStage.stageWidth - _resourceinfPanel.width;
			addChild(_playInfPanel);
			addChild(_resourceinfPanel)
			addChild(_test);
			addChild(_homeBackButton);
			addChild(_operateFt);
			
			//调试用代码，勿删 
			
			var canvas:Sprite = new Sprite();
			var bitmap:Bitmap = new Bitmap(new BitmapData(SceneConst.X_NUM*3,SceneConst.Z_NUM*3,false,0));
			bitmap.scaleX = bitmap.scaleY = 2;
//			bitmap.smoothing = true;
			bitmap.rotation = 45;
			canvas.addChild(bitmap);
			canvas.scaleY = 0.5;
			addChild(canvas);
			canvas.y =  App.appStage.stageHeight - canvas.height - 30;
			canvas.x = canvas.width/2;
			
			var timer:Timer = new Timer(100);
			timer.addEventListener(TimerEvent.TIMER,refrashMapCanvas);
			timer.start();
			function refrashMapCanvas(event:TimerEvent):void
			{
				var grid:MapGrid = AppControllerList.getInstance().sceneController.getGrid(2);
				bitmap.bitmapData.fillRect(bitmap.bitmapData.rect,0xffffff);
				for (var x:int = 0; x < grid.numCols; x++) 
				{
					for (var y:int = 0; y < grid.numRows; y++) 
					{
						var node:ANode = grid.getNode(x,y);
						var walkable:int = grid.getWalkable(x,y);
						if(node && node.marks && node.marks.length > 0)
						{
							bitmap.bitmapData.setPixel(x,y,0xff6600);
						}
						else if(walkable == 0)
						{
							bitmap.bitmapData.setPixel(x,y,0x000000);
						}else{
							bitmap.bitmapData.setPixel(x,y,0xffffff);
						}
					}
				}
				
			}
				
		}
		
		protected function onHomeClick(event:MouseEvent):void
		{
			dispatchEvent(new MainEvent(MainEvent.CLICK_HOME_BUTTON));
		}
		
		private function onResize(e:Event):void
		{
			upDataUI();
		}
		public function upDataInfo():void
		{
			_resourceinfPanel.updataResouseInfo();
			_playInfPanel.upDataPlayerInfo();
		}
		public function upDataUI():void
		{
			_resourceinfPanel.x = App.appStage.stageWidth - _resourceinfPanel.width;
			_test.x = (App.appStage.stageWidth - _test.width) / 2;
			_test.y = App.appStage.stageHeight - _test.height;
			_operateFt.x = App.appStage.stageWidth - _operateFt.width;
			_operateFt.y = App.appStage.stageHeight - _operateFt.height;
			/*_ppp.x = 200;
			_ppp.y = 200;*/
		}
		
		private var _friendList:Array;
		
		public function set friendList(data:Array):void
		{
			_friendList = data;
		}
		private var _playerObj:UserInfoObj;
		public function set playerObj(player:UserInfoObj):void
		{
			_playerObj = player;
		}
		public function get playerObj():UserInfoObj
		{
			return _playerObj;
		}
		
//		private var _friendList:Array;
		
//		public function set friendList(data:Array):void
//		{
//			_friendList = data;
//		}
		public function get friendList():Array{
			return _friendList;
		}
		public function addLoginBox():void{
			_loginBox = new LoginBox();
			_loginBox.x = (App.appStage.stageWidth - _loginBox.width) / 2;
			_loginBox.y = (App.appStage.stageHeight - _loginBox.width) / 2;
			addChild(_loginBox);
			_loginBox.addEventListener(MainEvent.CLICK_LOGIN_EVENT,_Login);
		}
		private function _Login(e:MainEvent):void{
			var loginEvent:MainEvent = new MainEvent(MainEvent.CLICK_LOGIN_EVENT,true,false,e.data);
			dispatchEvent(loginEvent);
		}
		
		public function showAllUI(value:Boolean):void
		{
			//屏蔽玩家信息面板
//			if(_playInfPanel)
//			_playInfPanel.visible = value;
			
			//屏蔽资源面板
//			if(_resourceinfPanel)
//			_resourceinfPanel.visible = value;
			
			//屏蔽测试面板
//			if(_test)
			_test.visible = value;
			
			//屏蔽回家按钮
//			if(_homeBackButton)
			_homeBackButton.visible = value;
			
			//屏蔽操作面板
//			if(_operateFt)
			_operateFt.visible = value;
		}
	}
}